#include <dirent.h>
#include <iostream>
#include <locale.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>


#include "brightness.h"
using namespace cv;
using namespace std;
/*********************************************************************************************************************************************************
 *函数描述： brightnessException 计算并返回一幅图像的色偏度以及，色偏方向
 *函数参数： InputImg 需要计算的图片，BGR存放格式，彩色（3通道），灰度图无效
 * cast
 *计算出的偏差值，小于1表示比较正常，大于1表示存在亮度异常；当cast异常时，da大于0表示过亮，da小于0表示过暗
 *函数返回值： 0，全黑，1偏暗，2，正常3，偏亮，4全白。
 **********************************************************************************************************************************************************/
Brightness brightnessException(cv::Mat InputImg) {
  float cast = 1.0, da = 0.0;
  Mat GRAYimg;

  cvtColor(InputImg, GRAYimg, CV_BGR2GRAY);
  // imshow("0",GRAYimg);
  // waitKey(0);
  // Mat temp = GRAYimg;
  // //二值化图像
  // threshold(temp,temp,10,255,THRESH_BINARY);
  // imshow("0",temp);
  // waitKey(0);
  // float count = countNonZero(temp);
  // float xx = count/temp.rows/temp.cols;

  // Mat temp1 = GRAYimg;
  // threshold(temp1,temp1,230,255,2);
  // float count1 = countNonZero(temp1);
  // float yy = count1/temp1.rows/temp1.cols;
  // imshow("0",temp1);
  // waitKey(0);

  float a = 0;
  int Hist[256];
  float b = 0; //计算全黑的个数
  float c = 0; //计算全白的个数
  for (int i = 0; i < 256; i++)
    Hist[i] = 0;
  for (int i = 0; i < GRAYimg.rows; i++) {
    for (int j = 0; j < GRAYimg.cols; j++) {

      a += float(GRAYimg.at<uchar>(i, j) -
                 128); //在计算过程中，考虑128为亮度均值点
      if (float(GRAYimg.at<uchar>(i, j)) < 30) {
        b++;
      }
      if (float(GRAYimg.at<uchar>(i, j)) > 220) {
        c++;
      }
      int x = GRAYimg.at<uchar>(i, j);
      Hist[x]++;
    }
  }
  // Hist存的是0-255中每一个像素的值。
  da = a / float(GRAYimg.rows * InputImg.cols); //每一个像素点亮度的平均值
  float xx = b / float(GRAYimg.rows * InputImg.cols); //每一个黑点的个数
  float yy = c / float(GRAYimg.rows * InputImg.cols); //每一个白点的个数
  float D = abs(da);                                  //求绝对值
  float Ma = 0;
  for (int i = 0; i < 256; i++) {
    Ma += abs(i - 128 - da) * Hist[i]; //求每一个像素点与平均值之间的差距
  }
  Ma /= float((GRAYimg.rows * GRAYimg.cols));
  float M = abs(Ma);
  float K = D / M;
  cast = K;
  cout << cast << endl;
  cout << "xx:" << xx << endl;
  cout << "yy:" << yy << endl;
  if (cast > 1) {
    if (da > 0) {
      if (yy > 0.90) {
        cout << "画面全亮！！！" << endl;
        return Brightness::COMPLETELY_WHITE;
      } else {
        cout << "画面偏亮！！！亮度值为: " << cast << endl;
        return Brightness::WHITE;
      }
    } else {
      if (xx > 0.90) {
        cout << "画面全黑！！！" << endl;
        return Brightness::COMPLETELY_DARK;
      } else {
        cout << "画面偏暗！！！亮度值为: " << cast << endl;
        return Brightness::DARK;
      }
    }
  }
  cout << "亮度正常，亮度值为: " << cast << endl;
  return Brightness::NORMAL;
}